//
//  YYMusicDetailHeader.swift
//  STMusic
//
//  Created by stoneobs on 17/4/18.
//  Copyright © 2017年 stoneobs. All rights reserved.
////**********************播放详情-header***************************

import UIKit

class YYMusicDetailHeader: UIView {

    //view 1
    var backView1:UIView! = UIView();
    var playNumLabel:STLabel?
    
    var downloadButton:STButton?
    
     var tagButton:STButton? //原创
    
     var titleLabel:STLabel?
    
     var typeLabel:STLabel? //视屏、音频
    
    //view 2
    var backView2:UIView! = UIView();
    var iconButton:STButton?
    var nameLable:STLabel?
    var personTypeLable:STLabel?//媒体人
    var fansNumLable:STLabel?
    var attentionButton:STButton?//关注
    var liricleLable:STLabel?
    
    //view3
    var backView3:UIView! = UIView();
    var commentTitleLable:STLabel?
    var scoreLabel:STLabel?;
    var scoreView:STXingXIngView?;
    
    
    //model
    
    var model:YYMusicDeatailModel? = YYMusicDeatailModel(){
    
        didSet{
        
            if let numLable = self.model?.works?.play_num {
             
                self.playNumLabel?.text = "播放 \(numLable) 次";
            }
           
            
            self.tagButton?.setTitle(self.model?.works?.category, for: .normal);
            
            self.titleLabel?.text = self.model?.works?.title;
            
            let array:Array<Dictionary<String,Any>> = self.model!.tags!;
            var titles:String? = "";
            for dic in array{
            
                if let tagTitle = dic["value"] {
                    titles = titles!  + " " + "#\(tagTitle)#";
                }
                
                
            }
            
            self.typeLabel?.text = titles;

            
           
            
            //view 2
            self.iconButton?.sd_setBackgroundImage(with: URL.init(string: (self.model?.user?.avatar)!), for: .normal, placeholderImage: nil, options: .retryFailed);
            
            self.nameLable?.text = self.model?.user?.nickname;
            let nickName:NSString = NSString.init(string: (self.model?.user?.nickname)!)
            self.nameLable?.width = nickName.widthWithheight(15, font: 15);
            if (self.nameLable?.width)! > CGFloat(200) {
                self.nameLable?.width = 200;
            }
            self.personTypeLable?.left = (self.nameLable?.right)! + 5 ;
                
            
            self.personTypeLable?.text = self.model?.user?.identity_tag;
            
            
            self.liricleLable?.text = self.model?.works?.words;
            
            let liricleTitle:NSString =  NSString.init(string: (self.liricleLable?.text)!);
            
            self.liricleLable?.height = liricleTitle.heigthWithwidth(SCREEN_WIDTH - 30, font: 13);
            
            self.backView2.height = (self.liricleLable?.bottom)! + 20;
            
            if let commentNumText = self.model?.comment?.total {
                
                  self.commentTitleLable?.text = "社区评论(\(commentNumText))";
            }
         
            
            self.commentTitleLable?.top = self.backView2.bottom;
            
            //view3
            
            if model?.works_score?["avg_score"] != nil {
                let avg = (model?.works_score?["avg_score"] as? NSNumber)?.floatValue;
               
                let avgStr =  String(format: "%.1f", avg!);
                self.scoreLabel?.text = "平均分"  + avgStr;
            }
  
            if model?.works_score?["is_scored"] != nil  &&  model?.works_score?["score_member"] != nil{
                self.scoreView?.difficultyNum = (model?.works_score?["is_scored"] as? Int)!;
                self.scoreView?.isUserInteractionEnabled = self.scoreView?.difficultyNum == 0 ? true:false;
            }
            
            self.backView3.top = (self.commentTitleLable?.bottom)!;
            
            self.height = self.backView3.bottom + 20;
            
            self.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: self.bounds);
            
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initSubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initSubview() {
        
        self.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: self.bounds);
        //view1;
        self.backView1 = UIView.init(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH, height: 200));
        self.backView1.backgroundColor = .white;
        self.playNumLabel = STLabel.init(frame: .init(x: 15, y: 20, width: 200, height: 13), text: "播放 1127次", textColor: FirstTextColor, font: 13, isSizetoFit: false  , textAlignment: .left);
        self.backView1.addSubview(self.playNumLabel!);
        self.downloadButton = STButton.init(frame: .init(x: SCREEN_WIDTH - 10 - 40, y: 20, width: 40, height: 20), title: "下载", titleColor: .white, titleFont: 13, cornerRadius: 3, backgroundColor: self.backgroundColor, backgroundImage: nil, image: nil);
        self.downloadButton?.contentHorizontalAlignment = .center;
        self.backView1.addSubview(self.downloadButton!);
        
        let lineView = UIView.init(frame: .init(x: 15, y: (self.playNumLabel?.bottom)! + 20, width: SCREEN_WIDTH - 30, height: 0.5));
        lineView.backgroundColor = LINE_COLOR;
        self.backView1.addSubview(lineView);
        
        self.tagButton = STButton.init(frame: .init(x: 15, y: lineView.bottom + 10, width: 40, height: 20), title: "原创", titleColor: .white, titleFont: 13, cornerRadius: 10, backgroundColor: self.backgroundColor, backgroundImage: nil, image: nil);
         self.tagButton?.contentHorizontalAlignment = .center;
        self.backView1.addSubview(self.tagButton!);
        
        self.titleLabel = STLabel.init(frame: .init(x: 15 + (self.tagButton?.right)!, y: lineView.bottom + 10, width: SCREEN_WIDTH - 15 - (15 + (self.tagButton?.right)!) , height: 15), text: "林忆莲 - 我不能忘记你", textColor: FirstTextColor, font: 15, isSizetoFit: false  , textAlignment: .left);
        self.titleLabel?.centerY = (self.tagButton?.centerY)!;
        self.backView1.addSubview(self.titleLabel!);
        
        self.typeLabel =  STLabel.init(frame: .init(x: 15 , y: (self.tagButton?.bottom)! + 10, width: 300, height: 35), text: "#视屏#", textColor: self.backgroundColor, font: 13, isSizetoFit: false  , textAlignment: .left);
        self.typeLabel?.numberOfLines = 0;
        self.backView1.addSubview(self.typeLabel!);
        
        self.backView1.height = (self.typeLabel?.bottom)! + 20;
        self.addSubview(self.backView1!);
        
        //view 2;
        self.backView2 = UIView.init(frame: .init(x: 0, y: self.backView1.bottom + 10, width: SCREEN_WIDTH, height: 300));
        self.backView2.backgroundColor = .white;
        
        self.iconButton = STButton.init(frame: .init(x: 15, y: 20, width: 40, height: 40), title: "", titleColor: .white, titleFont: 0, cornerRadius: 20, backgroundColor: .white, backgroundImage: UIImage.init(named: "4"), image: nil);
        self.iconButton?.contentHorizontalAlignment = .center;
        self.backView2.addSubview(self.iconButton!);
        self.iconButton?.showRoundShadow();
        
        self.nameLable = STLabel.init(frame: .init(x: 10 + (self.iconButton?.right)!, y: 20, width: 80, height: 15), text: "碎月娱乐", textColor: FirstTextColor, font: 15, isSizetoFit: false  , textAlignment: .left);
        self.backView2.addSubview(self.nameLable!);
        
        self.personTypeLable = STLabel.init(frame: .init(x: 5 + (self.nameLable?.right)!, y: lineView.bottom + 10, width: 100, height: 13), text: "媒体人", textColor: ThirdTextColor, font: 13, isSizetoFit: false  , textAlignment: .left);
        self.personTypeLable?.centerY = (self.nameLable?.centerY)!;
        self.backView2.addSubview(self.personTypeLable!);
        
        self.fansNumLable = STLabel.init(frame: .init(x: 5 + (self.nameLable?.right)!, y: lineView.bottom + 10, width: 60, height: 13), text: "1807粉丝", textColor: ThirdTextColor, font: 13, isSizetoFit: false  , textAlignment: .left);
        self.fansNumLable?.top = (self.nameLable?.bottom)! + 5;
        self.fansNumLable?.left = (self.nameLable?.left)!;
        self.backView2.addSubview(self.fansNumLable!);
        
        self.attentionButton = STButton.init(frame: .init(x:SCREEN_WIDTH -  15 - 70, y: 20, width: 70, height: 30), title: "关注", titleColor: FirstTextColor, titleFont: 14, cornerRadius: 0, backgroundColor: .white, backgroundImage:nil, image: nil);
        self.attentionButton?.contentHorizontalAlignment = .center;
        self.attentionButton?.centerY = (self.iconButton?.centerY)!;
        self.attentionButton?.setBrderWidth(0.5, borderColor: LINE_COLOR);
        self.backView2.addSubview(self.attentionButton!);

        let lineView2 = UIView.init(frame: .init(x: 15, y: (self.iconButton?.bottom)! + 20, width: SCREEN_WIDTH - 30, height: 0.5));
        lineView2.backgroundColor = LINE_COLOR;
        self.backView2.addSubview(lineView2);
        
        self.liricleLable = STLabel.init(frame: .init(x: 15 , y: lineView2.bottom + 20, width: SCREEN_WIDTH - 30, height: 100), text: "dsfsdfsdf", textColor: FirstTextColor, font: 13, isSizetoFit: false  , textAlignment: .left);
        self.liricleLable?.numberOfLines = 0;
        self.backView2.addSubview(self.liricleLable!);
        self.backView2.height = (self.liricleLable?.bottom)! + 20;
        self.addSubview(self.backView2);
        
        //view 3
        self.commentTitleLable =  STLabel.init(frame: .init(x: 15 , y: self.backView2.bottom, width: SCREEN_WIDTH - 30, height: 20), text: "社区评论（25）", textColor: .white, font: 13, isSizetoFit: false  , textAlignment: .left);
        self.commentTitleLable?.backgroundColor = .clear;
        self.addSubview(self.commentTitleLable!);
        
        self.backView3 = UIView.init(frame: CGRect.init(x: 0, y: (self.commentTitleLable?.bottom)!, width: SCREEN_WIDTH, height: 100));
        
        self.backView3.backgroundColor = .white;
        self.scoreLabel =  STLabel.init(frame: .init(x: 0 , y: 20, width: SCREEN_WIDTH , height: 13), text: "尚未得到足够的评分", textColor: FirstTextColor, font: 13, isSizetoFit: false  , textAlignment: .center);
        self.backView3.addSubview(self.scoreLabel!);
        
        self.scoreView = STXingXIngView.init(frame: .init(x: 0, y: (self.scoreLabel?.bottom)! + 20, width: 160, height: 15));
        self.scoreView?.centerX = SCREEN_WIDTH / 2;
        self.scoreView?.difficultyNum = 0;
        self.backView3.addSubview(self.scoreView!);
        self.backView3.height = (self.scoreView?.bottom)! + 20;
        self.addSubview(self.backView3!);
        
        self.height = self.backView3.bottom ;
        

        
        
    }

}
